package geektime.spring.springbucks;

import geektime.spring.springbucks.converter.BytesToMoneyConverter;
import geektime.spring.springbucks.converter.MoneyToBytesConverter;
import geektime.spring.springbucks.mapper.CoffeeMapper;
import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.convert.RedisCustomConversions;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.Arrays;
import java.util.List;
import java.util.TimeZone;


@Slf4j
@EnableTransactionManagement
@SpringBootApplication
@MapperScan("geektime.spring.springbucks.mapper")
@EnableRedisRepositories
public class DruidMybatisRedisDemoApplication implements ApplicationRunner {

    @Autowired
    private CoffeeMapper coffeeMapper;

    @Autowired
    private CoffeeService coffeeService;

    public static void main(String[] args) {
        SpringApplication.run(DruidMybatisRedisDemoApplication.class, args);
    }

    @Bean
    public RedisTemplate<String, Coffee> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Coffee> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    //        return builder -> {
//            builder.indentOutput(true);
//            builder.timeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//        };


    @Bean
    public RedisCustomConversions redisCustomConversions() {
        return new RedisCustomConversions(
                Arrays.asList(new MoneyToBytesConverter(), new BytesToMoneyConverter()));
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Coffee c = Coffee.builder().name("ruixin")
                .price(Money.of(CurrencyUnit.of("CNY"), 20.0)).build();
        coffeeService.saveCoffee(c);
        log.info("Save {} Coffee: {}", c);

        c = Coffee.builder().name("espresso")
                .price(Money.of(CurrencyUnit.of("CNY"), 25.0)).build();
        coffeeService.saveCoffee(c);
        log.info("Save {} Coffee: {}", c);

        c = Coffee.builder().name("latte")
                .price(Money.of(CurrencyUnit.of("CNY"), 30.0)).build();
        coffeeService.saveCoffee(c);
        log.info("Save {} Coffee: {}", c);

        c = Coffee.builder().name("mocha")
                .price(Money.of(CurrencyUnit.of("CNY"), 30.0)).build();
        coffeeService.saveCoffee(c);
        log.info("Save {} Coffee: {}", c);

        Coffee coffeess = coffeeService.getCoffeeById(1L);
        log.info("selectCoffeeById: {}", coffeess);

        c.setPrice(Money.of(CurrencyUnit.of("CNY"), 60.0));
        coffeeService.updateCoffee(c);
//        c.setPrice(Money.of(CurrencyUnit.of("CNY"), 60.0));
//        coffeeService.updateCoffeeById(c.getId());

        Long[] array = {1L, 2L, 3L, 4L};
        List<Long> coffeeIds = Arrays.asList(array);
        List<Coffee> coffees = coffeeService.selectCoffeeListByIds(coffeeIds);
        log.info("根据主键批量查询  {}", coffees);

        log.info("所有coffee2222  {}", coffees);
        int count = coffeeService.deleteCoffee(c);
        log.info("删除条数  {}", count);

        List<Coffee> coffees1 = coffeeService.selectAllCoffee();
        log.info("所有coffee  {}", coffees);
        coffeeService.selectCoffeeByName("ruixin");

        List<Coffee> allWithParam = coffeeService.findAllWithParam(1, 3);
        log.info("翻页查询结果 {}", allWithParam);
//        coffeeService.findOneCoffee("ruixin");
    }
//        Coffee c = Coffee.builder().name("ruixin")
//                .price(Money.of(CurrencyUnit.of("CNY"), 20.0)).build();
//        int count = coffeeMapper.save(c);
//        log.info("Save {} Coffee: {}", count, c);
//
//        CoffeeCache coffeeCache = coffeeCacheMapper.findByName("ruixin");
//        if (coffeeCache!=null) {
//            log.info("Coffee222 {} found in cache.", coffeeCache);
//        } else {
//            log.info("Coffee {} NOT found in cache.");
//            Coffee coffee = coffeeMapper.findByName("ruixin");
//            log.info("HAHAHA.", coffee);
//            if(coffee!=null){
//                log.info("HAH2222AHA.");
//                coffeeCache = CoffeeCache.builder()
//                        .id(coffee.getId())
//                        .name(coffee.getName())
//                        .price(coffee.getPrice())
//                        .build();
//                log.info("Save Coffee {} to cache.", coffeeCache);
//                coffeeCacheMapper.save(coffeeCache);
//            }
//        }
//    }
}
